<template>
    <!---搜索-->
     <el-form :inline="true" :model="formData" class="demo-form-inline">
        <el-form-item label="编号">
          <el-input v-model="formData.id" placeholder="请输入对应得编号" clearable />
        </el-form-item>
      <el-form-item label="类目名称">
              <el-input v-model="formData.name" placeholder="请输入类目得名称" clearable />
            </el-form-item>
     <el-form-item label="批量查询">
                  <el-input v-model="formData.ids" placeholder="请输入多个类目编号" clearable />
                </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
<!--类目列表-->
    <el-table :data="tableData" style="width: 100%">
        <el-table-column fixed prop="id" label="编号" width="100" />
        <el-table-column prop="name" label="类目名称" width="120" />
        <el-table-column prop="img" label="类目图片" width="120" >
            <!--模板插槽-->
            <template #default="scope">
                <img :src="scope.row.img" alt="" width="50px" height="50px">

            </template>
        </el-table-column>
        <el-table-column prop="seq" label="类目排序" width="120" />
        <el-table-column prop="status" label="状态" width="90" />
        <el-table-column prop="parentId" label="父目录" width="90" />
        <el-table-column fixed="right" label="操作" width="120">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="offineClick(scope.row)">下线</el-button
            >
            <el-button link type="primary" size="small" @click="handleClick(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          background
          :page-size="pageData.pageSize"
          layout="total, prev, pager, next"
          :total="pageData.total"
          @size-change="sizeChange"
          @current-change="currentChange"
        />
</template>
<script setup lang="ts">
import { categoryApi } from '@/api';
import { ref, onMounted, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router';
const router=useRouter()
const formData = reactive({
    id: '',
    name: '',
    ids:[]
})
const pageData = reactive({
    total: 0,
    pageSize: 3,
    pageNum:1
})


const tableData = ref([
    {
        date: '2016-05-03',
        name: 'Tom',
        state: 'California',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        zip: 'CA 90036',
        tag: 'Home',
    }
])
//生命周期得钩子函数 用来页面加载得时候立刻加载我得数据
onMounted(() => { 
    CategoryListApi()
})
//pageNum=1&pageSize=3
const CategoryListApi = () => { 
    let id = formData.id === '' ? undefined : formData.id
    let name = formData.name === '' ? undefined : formData.name
     let ids =formData.ids[0]===0 ? undefined : formData.ids
    categoryApi.select.call({
        id: id, name: name, ids: ids,
        pageNum: pageData.pageNum,
        pageSize:pageData.pageSize
    }).then((res: any) => { 
        console.log(res);
        tableData.value = res.items
        pageData.total=res.total
    })
 
}
const onSubmit = () => {
    console.log('submit!')
    //回调api接口数据
    CategoryListApi()
}
const sizeChange = (val: number) => {
    console.log(`${val} items per page`)
}
const currentChange = (num: number) => {
    console.log(`current page: ${num}`)
    pageData.pageNum = num
    //回调函数 更新数据
    CategoryListApi()
}

const handleClick = (row: any) => {
  console.log('click', row)
  //点击编辑跳转到编辑页面内
  router.push({ name: "category-edit", query: {id:row.id} })
}
const offineClick = (row: any) => {
  ElMessageBox.confirm(
    '您确定要对：?' + row.name + " 下线吗？",
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      categoryApi.delete.call({ id: row.id }).then((res) => {
        console.log(res);
        
        ElMessage({
          type: 'success',
          message: '恭喜您下线成功！祝您生意兴隆',
        })
        //刷新当前的数据
        CategoryListApi()
      })

    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消成功，感谢您手下留情',
      })
    })
}

// axios({
//     method: 'get',
//     url:'/api/category'
// }).then((res: any) => { 
//     //如果数据通了 需要回调把数据回传过来
//     console.log(res.data);
//     tableData.value=res.data.data
// }).catch((error: any) => { 
//     //如果以上url没有访问成功就是执行catch这个里面
//     console.log(error);
    
// }).finally(() => { 
//     console.log("无论是否报错都会执行得逻辑块");
    
// })

</script>
<style scoped>

</style>